﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views {
    import flash.events.*;
    import flash.display.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.interfaces.*;
    import net.wooga.pets.views.components.*;
    import net.wooga.pets.constants.*;
    import net.wooga.pets.utils.*;
    import net.wooga.pets.controllers.startup.*;
    import net.wooga.pets.views.components.controls.*;

    public class TruckMediator extends RenderListMediator implements ITicking {

		[Inject]
        public var truckView:TruckView;
		[Inject]
        public var tickModel:TickModel;
		[Inject]
        public var userModel:UserModel;
		[Inject]
        public var gameViewModel:GameViewModel;
		[Inject]
        public var languageModel:LanguageModel;
		[Inject]
        public var contractsModel:ContractsModel;
        private var _truckIcon:MovieClip;
        private var _readyIn:Number;
        private var _boxIcon:MovieClip;
        private var _textBubble:MovieClip;
        private var _textField:SpeechBubbleTextfield;

        override public function onRegister():void{
            super.onRegister();
            this.truckView.scaleX = (this.truckView.scaleY = GameConstants.HOSPITAL_SCALE);
            this.truckView.init();
            eventMap.mapListener(this.truckView, EventConstants.TRUCK_ARRIVED, this.onTruckArrived);
            eventMap.mapListener(eventDispatcher, EventConstants.CREATE_CONTRACT_COMPLETE, this.onContractCreated);
            eventMap.mapListener(eventDispatcher, EventConstants.INIT_WORLD_COMPLETE, this.initTruckAndSign);
            eventMap.mapListener(eventDispatcher, EventConstants.UPDATED_CONTRACT, this.updateState);
            eventMap.mapListener(eventDispatcher, EventConstants.KIOSK_AVAILABLE, this.onKioskAvailable);
            this._truckIcon = AssetsLibrary.getMovieClip("Goods_TruckBubbleIcon");
            this._boxIcon = AssetsLibrary.getMovieClip("Goods_BoxBubbleIcon");
        }
        private function onKioskAvailable(_arg1:Event):void{
            this.initTruckAndSign();
        }
        public function onTick(_arg1:Boolean=true, _arg2:Number=0):void{
            this._readyIn = (this.contractsModel.currentContract.readyAt - _arg2);
            if (this._readyIn <= 0){
                this.contractsModel.state = ContractsModel.DELIVER_GOODS;
            };
            var _local3:String = ((this.languageModel.getTextByKey("order_goods_mouseover_back_in") + "<br>") + TimeUtils.secondsToHoursMinSec(this._readyIn));
            this.showSpeechBubble(_local3, this._truckIcon);
        }
        private function initTruckAndSign(_arg1:Event=null):void{
            var _local2:Boolean;
            if ((((this.userModel.visitingFriendID == this.userModel.mySocialID)) && ((this.userModel.level >= GameConstants.MIN_LEVEL_KIOSK)))){
                _local2 = true;
                dispatch(new InfoEvent(EventConstants.INIT_CONTRACT));
            };
            this.handleTruckView(_local2);
        }
        private function handleTruckView(_arg1:Boolean):void{
            if (_arg1){
                this.truckView.addEventListener(MouseEvent.CLICK, this.onMouseClick);
                this.truckView.addEventListener(MouseEvent.MOUSE_OVER, this.onMouseOver);
                this.truckView.addEventListener(MouseEvent.MOUSE_OUT, this.onMouseOut);
                eventMap.mapListener(eventDispatcher, EventConstants.UPDATE_RENDER_LIST, updateRenderList);
                eventMap.mapListener(eventDispatcher, EventConstants.UPDATE_RENDER_LIST_FOR_DRAGGING, updateRenderListForDragging);
            } else {
                this.truckView.removeEventListener(MouseEvent.CLICK, this.onMouseClick);
                this.truckView.removeEventListener(MouseEvent.MOUSE_OVER, this.onMouseOver);
                this.truckView.removeEventListener(MouseEvent.MOUSE_OUT, this.onMouseOut);
                eventMap.unmapListener(eventDispatcher, EventConstants.UPDATE_RENDER_LIST, updateRenderList);
                eventMap.unmapListener(eventDispatcher, EventConstants.UPDATE_RENDER_LIST_FOR_DRAGGING, updateRenderListForDragging);
            };
            this.truckView.visible = _arg1;
        }
        private function updateState(_arg1:InfoEvent):void{
            Displays.removeAllChildren(this.truckView.speechBubbleContainer);
            Displays.removeAllChildren(this._textBubble);
            switch (_arg1.id){
                case ContractsModel.WAITING:
                    this.waitingForContract();
                    break;
                case ContractsModel.DELIVER_GOODS:
                    this.deliverGoods();
                    break;
                case ContractsModel.READY_TO_FINALIZE:
                    dispatch(new InfoEvent(EventConstants.PLAY_EFFECT_SOUND, GameConstants.SOUND_DOOR_CLOSES));
                    this.readyToFinalize();
                    break;
                case ContractsModel.READY_TO_CREATE:
                    this.readyToCreate();
                    break;
            };
        }
        private function waitingForContract():void{
            if (this.contractsModel.currentContract.readyAt <= this.tickModel.timestamp){
                this.contractsModel.state = ContractsModel.DELIVER_GOODS;
            } else {
                dispatch(new TickingEvent(TickingEvent.ADD_TICKING, this, TimeConstants.SECOND));
            };
        }
        private function deliverGoods():void{
            dispatch(new TickingEvent(TickingEvent.REMOVE_TICKING, this));
            this.truckView.deliverGoods();
        }
        private function readyToFinalize():void{
            this.truckView.speechBubbleContainer.visible = true;
            this.showIconBubble(this._boxIcon);
        }
        private function readyToCreate():void{
            this.truckView.parkTruckDesideHospital();
        }
        private function onContractCreated(_arg1:Event):void{
            this.truckView.sendTruckAway();
            dispatch(new InfoEvent(EventConstants.INIT_CONTRACT));
        }
        private function onContractFinalized():void{
            this.contractsModel.state = ContractsModel.READY_TO_CREATE;
        }
        private function onTruckArrived(_arg1:Event):void{
            this.truckView.parkTruckDesideHospital(false);
            this.contractsModel.state = ContractsModel.READY_TO_FINALIZE;
        }
        private function onMouseClick(_arg1:MouseEvent):void{
            switch (this.contractsModel.state){
                case ContractsModel.CREATING:
                case ContractsModel.WAITING:
                case ContractsModel.DELIVER_GOODS:
                    break;
                case ContractsModel.READY_TO_FINALIZE:
                    Displays.removeAllChildren(this.truckView.speechBubbleContainer);
                    dispatch(new InfoEvent(EventConstants.FINALIZE_CONTRACT));
                    this.onContractFinalized();
                    break;
                case ContractsModel.READY_TO_CREATE:
                    Displays.removeAllChildren(this.truckView.speechBubbleContainer);
                    dispatch(new InfoEvent(EventConstants.CREATE_POPUP, GameConstants.GOODS_POPUP, null));
                    break;
            };
        }
        private function onMouseOver(_arg1:MouseEvent):void{
            var _local2:String;
            this.truckView.speechBubbleContainer.visible = true;
            this.truckView.filters = [Filters.getFilter(Filters.MACHINE_GLOW)];
            switch (this.contractsModel.state){
                case ContractsModel.WAITING:
                case ContractsModel.DELIVER_GOODS:
                    Displays.removeAllChildren(this.truckView.speechBubbleContainer);
                    _local2 = ((this.languageModel.getTextByKey("order_goods_mouseover_back_in") + "<br>") + TimeUtils.secondsToHoursMinSec(this._readyIn));
                    this.showSpeechBubble(_local2, this._truckIcon);
                    break;
                case ContractsModel.READY_TO_FINALIZE:
                    break;
                case ContractsModel.READY_TO_CREATE:
                    Displays.removeAllChildren(this.truckView.speechBubbleContainer);
                    this.showSpeechBubble(this.languageModel.getTextByKey("order_goods_mouseover_click"), this._truckIcon);
                    break;
            };
        }
        private function onMouseOut(_arg1:MouseEvent):void{
            this.truckView.filters = [];
            switch (this.contractsModel.state){
                case ContractsModel.READY_TO_FINALIZE:
                    break;
                default:
                    this.truckView.speechBubbleContainer.visible = false;
                    Displays.removeAllChildren(this.truckView.speechBubbleContainer);
                    break;
            };
        }
        private function showSpeechBubble(_arg1:String, _arg2:MovieClip=null):void{
            this._textBubble = AssetsLibrary.getMovieClip(SpeechBubbleConstants.PET_CUSTOM_BUBBLE);
            this._textField = ((this._textField) || (new SpeechBubbleTextfield("0x683304", 60)));
            this._textField.htmlText = _arg1;
            this._textBubble.background.width = ((SpeechBubbleConstants.GOODS_BUBBLE_MARGIN_LEFT + this._textField.width) + SpeechBubbleConstants.GOODS_BUBBLE_MARGIN_RIGHT);
            this._textBubble.background.height = (((SpeechBubbleConstants.GOODS_BUBBLE_MARGIN_TOP_WITH_ICON + this._textField.height) + SpeechBubbleConstants.PET_BUBBLE_MARGIN_BOTTOM) + SpeechBubbleConstants.PET_BUBBLE_OFFSET_BOTTOM);
            this._textField.x = SpeechBubbleConstants.GOODS_BUBBLE_MARGIN_LEFT;
            this._textField.y = (-(this._textBubble.background.height) + SpeechBubbleConstants.GOODS_BUBBLE_MARGIN_TOP_WITH_ICON);
            if (_arg2){
                _arg2.x = ((this._textBubble.background.width / 2) - (_arg2.width / 2));
                _arg2.y = ((-(this._textBubble.background.height) + SpeechBubbleConstants.GOODS_BUBBLE_MARGIN_TOP_WITH_ICON) - _arg2.height);
                this._textBubble.addChild(_arg2);
            };
            this._textBubble.scaleX = (this._textBubble.scaleY = (1.5 / this.gameViewModel.gameViewScale));
            this._textBubble.addChild(this._textField);
            this.truckView.speechBubbleContainer.addChild(this._textBubble);
        }
        private function showIconBubble(_arg1:MovieClip):void{
            this._textBubble = AssetsLibrary.getMovieClip(SpeechBubbleConstants.PET_CUSTOM_BUBBLE);
            this._textBubble.background.width = ((SpeechBubbleConstants.GOODS_BUBBLE_MARGIN_LEFT + _arg1.width) + SpeechBubbleConstants.GOODS_BUBBLE_MARGIN_RIGHT);
            this._textBubble.background.height = (((SpeechBubbleConstants.GOODS_BUBBLE_MARGIN_TOP + _arg1.height) + SpeechBubbleConstants.PET_BUBBLE_MARGIN_BOTTOM) + SpeechBubbleConstants.PET_BUBBLE_OFFSET_BOTTOM);
            _arg1.x = ((this._textBubble.background.width / 2) - (_arg1.width / 2));
            _arg1.y = (((-(this._textBubble.background.height) / 2) - (_arg1.height / 2)) - (SpeechBubbleConstants.PET_BUBBLE_OFFSET_BOTTOM / 2));
            this._textBubble.addChild(_arg1);
            this._textBubble.scaleX = (this._textBubble.scaleY = (1.5 / this.gameViewModel.gameViewScale));
            this.truckView.speechBubbleContainer.addChild(this._textBubble);
        }

    }
}//package net.wooga.pets.views 
